/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package mf.demo.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import mf.demo.IService;
import mf.demo.User;
import mf.mfrpc.serialize.DataSource;
import mf.mfrpc.serialize.DataSourceIterator;
import mf.mfrpc.serialize.FileDataSource;
import mfinder.annotation.WebService;

/**
 * copy the methods from
 * @see mf.demo.server.RPCServerAction
 * 
 */
@WebService(name = "/ws/test", interfaceClass = {IService.class})
public class WebServiceAction implements IService, Serializable {

    @Override
    public Object testObject() {
        User u = new User("admin", "admin");
        Map map = new HashMap();
        map.put("string", "ok");
        map.put("int", 100);
        map.put("boolaen", true);
        map.put("string[]", new String[]{"the end"});
        map.put("user", u);
        return map;
    }

    @Override
    public User testBean(String name, String pwd) {
        User u = new User(name, pwd);
        return u;
    }

    @Override
    public InputStream testInputStream(String filepath) throws IOException {
        File f = new File(filepath);
        return f.exists() ? new FileInputStream(f) : null;
    }

    @Override
    public DataSource testDataSource(String filepath) {
        File f = new File(filepath);
        return f.exists() ? new FileDataSource(f) : null;
    }

    @Override
    public void testUploadDataSource(DataSource data) throws IOException {
        System.out.println(data.getName() + "," + data.getSize() + "," + data.getLastModified());
        System.out.println("==============================");
        System.out.println(getStream(data.getInputStream()));
        System.out.println("==============================");
    }

    @Override
    public String testUploadDataSources(DataSourceIterator it) throws IOException {

        if (it != null) {
            while (it.hasNext()) {
                DataSource data = it.next();
                InputStream in = data.getInputStream();
                System.out.println("------------------------------");
                System.out.println(data.getName() + "," + data.getSize());
                System.out.println("==============================");
                System.out.println(getStream(in));
                System.out.println("==============================");
            }
        }
        return "OK";
    }

    //test
    private static String getStream(InputStream in) throws IOException {
        byte[] response = new byte[in.available()];
        in.read(response);
        in.close();
        return new String(response, "utf-8").trim();
    }
}
